/*
 * @(#)ExcelTextExtractor.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.dms.extractor;

import java.io.InputStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

/**
  ExcelTextExtractor.java

  This class is the text extractor for Excel files.

    @author          Rollo Chan
    @company         DCIVision Limited
    @creation date   26/08/2003
    @version         $Revision: 1.7.32.1 $
*/

public class ExcelTextExtractor extends AbstractTextExtractor {

  public static final String REVISION = "$Revision: 1.7.32.1 $";
  private static final Log log = LogFactory.getLog(ExcelTextExtractor.class);
  
  public ExcelTextExtractor(InputStream fis, String encoding) {
    super(fis, encoding);
    if (fis==null) {
      return;
    }

    try {
      StringBuffer sb = new StringBuffer();

/*
      HSSFWorkbook workbook = new HSSFWorkbook(fis);

      int numOfSheets = workbook.getNumberOfSheets();

      for(int i = 0; i < numOfSheets; i++) {
        HSSFSheet sheet = workbook.getSheetAt(i);

        Iterator rowIterator = sheet.rowIterator();

        while (rowIterator.hasNext()) {
          HSSFRow row = (HSSFRow)rowIterator.next();

          Iterator cellIterator = row.cellIterator();

          while (cellIterator.hasNext()) {
            HSSFCell cell = (HSSFCell)cellIterator.next();

            String cellStringValue = null;

            if (cell.getCellType() == 4) {
              boolean booleanValue = cell.getBooleanCellValue();
              cellStringValue = Boolean.toString(booleanValue);
            } else if (cell.getCellType() == 0) {
              double doubleValue = cell.getNumericCellValue();
              cellStringValue = Double.toString(doubleValue);
            } else if (cell.getCellType() == 1) {
              cellStringValue = cell.getStringCellValue();
            }

            if (cellStringValue != null) {
              sb.append(cellStringValue);
              sb.append("\t");
            }
          }

          sb.append("\n");
        }
      }
*/

      Workbook workbook = Workbook.getWorkbook(fis);

      int numOfSheets = workbook.getNumberOfSheets();
      for (int i = 0; i < numOfSheets; i++) {
        Sheet sheet = workbook.getSheet(i);
        for (int j = 0; j < sheet.getRows(); j++) {
          Cell[] cellAry = sheet.getRow(j);
          for (int k = 0; k < cellAry.length; k++) {
            sb.append(cellAry[k].getContents() + "\t");
          }
          sb.append("\n");
        }
      }

      text = sb.toString();
    } catch (Exception e) {
      log.error(e, e);
    }
  }

}
